﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ServiceModel;
using System.Windows.Threading;
namespace P2P_Social_networking
{
    /// <summary>
    /// Interaction logic for msgSend.xaml
    /// </summary>
    public partial class msgSend : Window
    {
        PeerClient myClient;
        public msgSend(PeerClient clientConnection)
        {
            InitializeComponent();
            myClient = clientConnection;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            myClient.sendMessage(themessage.Text, theperson, Environment.UserName);
            this.Visibility = System.Windows.Visibility.Collapsed;
        }
        Int32 currentItem;
        Int32 canvasY;
        void scanPeople(Object sender, EventArgs e)
        {
            App thecontext = (App)App.Current;
            if (currentItem < thecontext.userList.Count)
            {
                TextBlock myBlock = new TextBlock();
                myBlock.Text = thecontext.userList[currentItem].Substring(0,thecontext.userList[currentItem].IndexOf(":"));
                Canvas.SetTop(myBlock, double.Parse(canvasY.ToString()));

                myBlock.Name = "caller" + thecontext.userList[currentItem].Substring(thecontext.userList[currentItem].IndexOf(":") + 1);
                myBlock.MouseLeftButtonDown += new MouseButtonEventHandler(myBlock_MouseLeftButtonDown);
                people.Children.Add(myBlock);
                canvasY = canvasY + 20;
                currentItem = currentItem + 1;

            }
            
            
        }
        Int32 theperson;
        void myBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Code to run when the user clicks on a user
            TextBlock theblock = (TextBlock)sender;
            theperson = Convert.ToInt32(theblock.Name.Substring("caller".Length));
            
            personSelector.Visibility = System.Windows.Visibility.Collapsed;
            people.Children.RemoveRange(0, people.Children.Count);
            currentItem = 0;
            canvasY = 0;
            toblock.Text = "Send message to " + theblock.Text;
        }
        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            personSelector.Visibility = System.Windows.Visibility.Visible;
           
        }

        private void Canvas_Loaded(object sender, RoutedEventArgs e)
        {
            personSelector.Visibility = System.Windows.Visibility.Visible;
            DispatcherTimer mytime = new DispatcherTimer();
            mytime.Tick += new EventHandler(scanPeople);
            mytime.Interval = TimeSpan.FromMilliseconds(50);
            mytime.Start();
        }
    }
}
